//
//  DocumentCell.m
//  KurtulusLaw
//
//  Created by phuc phuc on 12/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DocumentCell.h"
#import "KDocumentDTO.h"

@implementation DocumentCell
@synthesize fileTypeImageButton3;
@synthesize fileTypeImageButton4;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setSearchFileDTO:(KBaseSearchFileDTO *)searchFile {
    [self.fileTypeImageButton setHidden:YES];
    [self.fileTypeImageButton3 setHidden:YES];
    [self.fileTypeImageButton4 setHidden:YES];
	
	[super setSearchFileDTO:searchFile];
	
	if ([searchFile isKindOfClass:[KDocumentDTO class]]) {
		_buttons = [[NSMutableArray alloc] init];
         [_buttons addObject:self.fileTypeImageButton];
         [_buttons addObject:self.fileTypeImageButton3];
         [_buttons addObject:self.fileTypeImageButton4];
         [_buttons addObject:self.fileTypeImageButton4];
         [_buttons addObject:self.fileTypeImageButton4];
		[_buttons addObject:self.fileTypeImageButton4];
		[_buttons addObject:self.fileTypeImageButton4];
		
        NSInteger count = 0;
		KDocumentDTO *documentDTO = (KDocumentDTO *)searchFile;
		
		
		if (documentDTO.filePDF && [documentDTO.filePDF length] > 0) {
            UIButton *btn = [_buttons objectAtIndex:count++];
			[btn setImage:[UIImage imageNamed:kIconPDF] forState:UIControlStateNormal];
            [btn setHidden:NO];
            [btn setTag:eFileTypePDF];
		} 
        if (documentDTO.pptFile && [documentDTO.pptFile length] > 0) {
            UIButton *btn = [_buttons objectAtIndex:count++];
			[btn setImage:[UIImage imageNamed:kIconPPT] forState:UIControlStateNormal];
            [btn setHidden:NO];
            [btn setTag:eFileTypePPT];
		}
        
        if (documentDTO.xlsFile && [documentDTO.xlsFile length] > 0) {
            UIButton *btn = [_buttons objectAtIndex:count++];
			[btn setImage:[UIImage imageNamed:kIconXLS] forState:UIControlStateNormal];
            [btn setHidden:NO];
            [btn setTag:eFileTypeXLS];
		}
        if (documentDTO.mp3File && [documentDTO.mp3File length] > 0) {
            UIButton *btn = [_buttons objectAtIndex:count++];
			[btn setImage:[UIImage imageNamed:kIconMP3] forState:UIControlStateNormal];
            [btn setHidden:NO];
            [btn setTag:eFileTypeMP3];
		}
        
        if (documentDTO.flvFile && [documentDTO.flvFile length] > 0) {
            UIButton *btn = [_buttons objectAtIndex:count++];
			[btn setImage:[UIImage imageNamed:kIconConvertingVideo] forState:UIControlStateNormal];
            [btn setHidden:NO];
            [btn setTag:eFileTypeConvertingVideo];
			
			if (documentDTO.fileMp4 && [documentDTO.fileMp4 length] > 0) {
				[btn setImage:[UIImage imageNamed:kIconConvertedVideo] forState:UIControlStateNormal];
				[btn setHidden:NO];
				[btn setTag:eFileTypeConvertedVideo];
			} else {
				[btn setHidden:YES];
			}

		}
        
        if (documentDTO.docFile && [documentDTO.docFile length] > 0) {
            UIButton *btn = [_buttons objectAtIndex:count++];
			[btn setImage:[UIImage imageNamed:kIconWord] forState:UIControlStateNormal];
            [btn setHidden:NO];
            [btn setTag:eFileTypeDOC];
		} 
        
        if (documentDTO.imgFile && [documentDTO.imgFile length] > 0) {
            UIButton *btn = [_buttons objectAtIndex:count++];
			[btn setImage:[UIImage imageNamed:kIconIMG] forState:UIControlStateNormal];
            [btn setHidden:NO];
            [btn setTag:eFileTypeIMG];
		}
		
				
    }
}

- (IBAction)touchInsidePDFImageButton:(id)sender {
	if (self.delegate) {
		NSString *relativeFile = @"";
		NSInteger type;
		KDocumentDTO *documentDTO = (KDocumentDTO *)self.searchFileDTO;
		
		UIButton *btn = (UIButton *)sender;
		
		switch (btn.tag) {
			case eFileTypePDF:
				relativeFile = [documentDTO.filePDF copy];
				type = eFileTypePDF;
				break;
			case eFileTypePPT:
				type = eFileTypePPT;
				relativeFile = [documentDTO.pptFile copy];
				break;
			case eFileTypeMP3:				
				type = eFileTypeMP3;
				relativeFile = [documentDTO.mp3File copy];
				break;
			case eFileTypeXLS:
				type = eFileTypeXLS;
				relativeFile = [documentDTO.xlsFile copy];
				break;
			case eFileTypeConvertingVideo:
				type = eFileTypeConvertingVideo;
				relativeFile = [documentDTO.flvFile copy];
				break;
			case eFileTypeDOC:
				
				type = eFileTypeDOC;
				relativeFile = [documentDTO.docFile copy];
				break;
			case eFileTypeIMG:
				type = eFileTypeIMG;
				relativeFile = [documentDTO.imgFile copy];

				break;
			case eFileTypeConvertedVideo:
				type = eFileTypeConvertedVideo;
				relativeFile = [documentDTO.fileMp4 copy];
				break;
			default:
				return;
				break;
		}
				
		[self.delegate searchFileCell:self clickDocumentFile:type relativePath:[relativeFile copy]];
	}
}

@end
